const { app, BrowserWindow, ipcMain, Notification, Menu } = require("electron");
const path = require("path");
const electron = require("electron");

const downloadService = require("./down");
const updateService = require("./update");
//const electronLog = require('electron-log')

const menuTemplate = require("./menuTemplate");

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require("electron-squirrel-startup")) {
  // eslint-disable-line global-require
  app.quit();
}
let mainWindow;
const createWindow = () => {
  // Create the browser window.
  mainWindow = new BrowserWindow({
    width: 1024,
    height: 768,
    webPreferences: {
      nodeIntegration: true,
      enableRemoteModule: true,
    },
    backgroundColor: "#f5f5f5",
    show: false,
  });

  // and load the index.html of the app.
  mainWindow.loadFile(path.join(__dirname, "index.html"));

  // Open the DevTools.
  mainWindow.webContents.openDevTools();

  //createView();

  mainWindow.on("closed", () => {
    mainWindow = null;
  });

  mainWindow.on("ready-to-show", () => {
    mainWindow.show();
    mainWindow.focus();
  });
};

const createView = () => {
  const BrowserView = electron.BrowserView;
  const view = new BrowserView();
  mainWindow.setBrowserView(view);
  view.setBounds({
    x: 0,
    y: 300,
    width: 800,
    height: 600,
  });
  console.log("xxxx");

  //view.webContents.loadURL("https://www.baidu.com");
};

const showMenu = () => {
  const menu = Menu.buildFromTemplate(menuTemplate);
  Menu.setApplicationMenu(menu);
};

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on("ready", () => {
  createWindow();
  //showNotify();
  // downloadService.downloadFile({ mainWindow });
  // showMenu();
  updateService.update({
    mainWindow,
  });
});

app.on("activate", () => {
  console.log("activate");
});

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

app.on("activate", () => {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});

ipcMain.on("openDevTools", (event, args) => {
  console.log("ipcMain", "openDevTools");
  mainWindow.webContents.openDevTools();
});

// 失败了,原因不明
const showNotify = () => {
  console.log("Notification.isSupported=", Notification.isSupported());
  // 实例化不会进行通知
  // let notification = new Notification({
  //   // 通知的标题, 将在通知窗口的顶部显示
  //   title: "Boss",
  //   // 通知的副标题, 显示在标题下面 macOS
  //   subtitle: "重要消息",
  //   // 通知的正文文本, 将显示在标题或副标题下面
  //   body: "@所有人 放假！！！",
  //   // false有声音，true没声音
  //   silent: false,
  //   icon: "./nm.jpg",
  //   // 通知的超时持续时间 'default' or 'never'
  //   timeoutType: "default",
  // });
  // console.log("show notification ");
  // // 向用户展示 notification
  // const aa = notification.show();
  // console.log("notification", aa);
};

process.on("uncaughtException", function (err) {
  console.error(err);
  //electronLog.error(err.stack);
});

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.
